<?php
/**
 * Created by Bruce.
 * IDE: PhpStorm
 * Date: 2018/8/8
 * Time: 9:37
 */

namespace app\park\controller;


use app\park\model\AdminQueryParkInfoMode;
use think\Request;

class AdminQueryParkInfo
{
    private $openId;

    public function query(Request $request)
    {
        $this->openId = $request->post('openId');
        if ($this->checkArgs()) {
            $mode = new AdminQueryParkInfoMode;
            $res = $mode->query($this->openId);
            return $res;
        } else {
            return ['code' => '300', 'result' => 'openId in null'];
        }

    }

    public function upData(Request $request)
    {
        $this->openId = $request->post('openId');
        $parkName = $request->post('parkName');//长度最大16
        $univalence = $request->post('univalence');//单价
        $longitude = $request->post('longitude');//经度
        $latitude = $request->post('latitude');//纬度
        $location = $request->post('location');//地址 最大长度32
        $space = $request->post('space');//车位总数
        if ($this->checkArgs()) {
            $args = ['univalence' => $univalence,
                'park_name' => $parkName,
                'longitude' => $longitude,
                'latitude' => $latitude,
                'location' => $location,
                'space' => $space];
            $response = $this->upDateCheck($args);
            if (empty($response)) {//参数正确
                $mode = new AdminQueryParkInfoMode();
                $res = $mode->upData($this->openId, $args);
                return $res;
            } else {
                return ['code' => '300', 'result' => $response];
            }

        } else {
            return ['code' => '300', 'result' => 'openId in null'];
        }


//        mb_strlen($this->cause, 'UTF-8')//检查字符串长度
    }

    private function checkArgs()
    {
        if (empty($this->openId)) {
            return false;
        } else {
            return true;
        }
    }

    private function upDateCheck(array $data)
    {
        $response = '';
        if (empty($data['park_name']) || mb_strlen($data['park_name'], 'UTF-8') > 17) {
            $response .= 'parkName error';
        } else if (!is_numeric($data['univalence'])) {
            $response .= 'univalence is error';
        } else if (!is_numeric($data['longitude'])) {
            $response .= 'longitude is null';
        } else if (!is_numeric($data['latitude'])) {
            $response .= 'latitude is null';
        } else if (empty($data['location']) || mb_strlen($data['location'], 'UTF-8') > 32) {
            $response .= 'location error';
        } else if (!is_numeric($data['space'])) {
            $response .= 'space error';
        }
        return $response;
    }
}